#connding:utf-8

import requests
import json


def get_info_json():
    song_search_url = 'https://songsearch.kugou.com/song_search_v2?' \
                      'callback=jQuery1124027331113823236675_1559647295775&' \
                      'keyword=%E5%86%AF%E6%8F%90%E8%8E%AB&' \
                      'page=1&' \
                      'pagesize=30&' \
                      'userid=-1&' \
                      'clientver=&' \
                      'platform=WebFilter&' \
                      'tag=em&' \
                      'filter=2&' \
                      'iscorrection=1&' \
                      'privilege_filter=0&' \
                      '_=1559647295777'

    conn = requests.get(song_search_url)
    conn.encoding='utf-8'
    conn = conn.text
    index = conn.find('(') + 1
    conn = conn[index:-2]
    print(conn)
    # jsonStr = json.loads(conn, encoding='utf-8')

    return conn

def save_json(jsonStr, fileName=None):
    if not fileName:
        import time
        fileName = str(int(time.time())) + '.json'

    try:
        with open(fileName, 'w', encoding='utf-8') as f:
            f.write(jsonStr)
        return True
    except Exception:
        return False

def json_print(jsonStr):
    jsonStr = json.loads(jsonStr, encoding='utf-8')
    music_lists = jsonStr['data']['lists']
    for i in music_lists:
        print(i.get('SongName'))
        print(i.get('HQExtName'))
        # print(i.get('ExtName'))
        print(i.get('Audioid'))
        print(i.get('FileHash'))
        print(i.get('FileName'))
        print('='*23)

if __name__ == '__main__':
    # jsonStr = get_info_json()
    # save_json(jsonStr, 'info.json')
    f = open('info.json', 'r', encoding='utf-8')
    jsonStr = f.read()
    json_print(jsonStr)
    f.close()

